<template>
	<view class="uni-content">
		<!-- 顶部文字 -->
		<text class="uni-content-title-line mt10 mb20">完善学生信息</text>
		<!-- 基础表单校验 -->
		<uni-forms ref="stuInfoForm" :modelValue="formData" labelPosition="left">
			<uni-forms-item label="区域">
				<uni-data-select v-model="regionCode" :localdata="[{ value: '220000', text: '吉林' }]"
					placeholder="请选择区域" :clear="false" disabled></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="输入分数" name="score"
				:rules="[{'required': true,errorMessage: '分数不能为空'},{ format: 'number', errorMessage: '必须是数字' },
				{ maximum: 750, errorMessage: '分数最高{maximum}' },{ minimum: 0, errorMessage: '分数最低{minimum}' }]">
				<uni-easyinput v-model="formData.score" placeholder="请输入您的分数" type="number"
					@blur="changeScore" maxlength="4" :clearable="false" />
			</uni-forms-item>
			<uni-forms-item label="首选科目" name="primarySubject" :rules="[{required: true,errorMessage: '首选科目不能为空'}]">
				<uni-data-checkbox v-model="formData.primarySubject" mode="tag" :localdata="primarySubjects"
					@change="getUserRank">
				</uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item label="再选科目" name="secondaryArray"
				:rules="[{required: true,errorMessage: '再选科目不能为空'},{minLength: 2,errorMessage: '再选科目必须是2个'}]">
				<uni-data-checkbox v-model="formData.secondaryArray" :max="2" mode="tag" multiple
					:localdata="secondarySubjects" @change="getUserRank">
				</uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item label="排名" name="rankNum"
				:rules="[{'required': true,errorMessage: '排名不能为空'},{ format: 'number', errorMessage: '必须是数字' }]">
				<uni-easyinput v-model="formData.rankNum" maxlength="6" type="number" placeholder="请输入您的排名"
					:clearable="false" />
			</uni-forms-item>
			<button class="orange-btn mt10 mb20" type="default" @click="submitForm()">提 交</button>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				regionCode: '220000',
				formData: {
					id: "",
					regionCode: "",
					score: "",
					bizUniSubject: ""
				},
				userRank: {},
				userRankLoading: false,
				primarySubjects: [],
				secondarySubjects: [],
			}
		},
		onLoad: function(option) {
			//参数赋值
			this.getUserInfo()
			//获取文理科字典数据
			this.$dict('biz_primary_subject').then((res) => {
				this.primarySubjects = res
			})
			this.$dict('biz_secondary_subject').then((res) => {
				this.secondarySubjects = res
			})
		},
		onShow() {
			this.$isLogin(true)
		},
		methods: {
			getUserInfo() {
				this.$syncUserInfo().then(() => {
					var stuInfo = uni.getStorageSync('gkms-stuInfo');
					if (stuInfo != null) {
						this.stuInfo = stuInfo
						Object.assign(this.formData, stuInfo)
						if (this.formData.secondarySubject) {
							this.formData.secondaryArray = [this.formData.secondarySubject.substring(0,
								1) + '00', this.formData.secondarySubject.substring(2, 3) + '00'
							]
							
						}
						this.$nextTick(() => {
							if (this.stuInfo != null && this.stuInfo.score != null) {
								this.getUserRank()
							}
						})
					}
				})
			},
			// 提交表单
			submitForm() {
				var toastMessage = '保存中，请稍后'
				this.$refs['stuInfoForm'].validate().then(res => {
					uni.showToast({
						icon: 'loading',
						title: toastMessage,
						duration: 20000
					});
					// 次选科目代码
					this.formData.secondarySubject = this.formData.secondaryArray[0].substring(0,
						1) + '1' + this.formData.secondaryArray[1].substring(0, 1)
					this.$post(this.$api.system.user.profile.update, {vltJson:this.formData}).then(res => {
						const vltForm = this.formData
						uni.hideToast()
						let vltStr = ''
						if (vltForm.primarySubject === 'WP') {
							vltStr = '物'
						} else {
							vltStr = '历'
						}
						const s2 = vltForm.secondarySubject.substring(0, 1)
						const s3 = vltForm.secondarySubject.substring(2, 3)
						const sArray = ['', '化', '生', '政', '地']
						vltStr += sArray[s2] + sArray[s3]
						vltForm.subjectName = vltStr
						uni.redirectTo({
							url: '/pages/ucenter/userinfo/index'
						});

					})
				}).catch(err => {
					console.log('err', err);
				})
			},
			
			// 查询排名
			getUserRank() {
				this.$refs['stuInfoForm'].validate().then(res => {
					this.userRankLoading = true
					if (!this.formData.score || !this.formData.primarySubject) {
						return
					}
					this.$get(this.$api.web.score.getRank, {
						score: this.formData.score,
						subjectCode: this.formData.primarySubject
					}).then(res => {
						this.userRank = res.data
						this.formData.rankNum = res.data.rank
						this.userRankLoading = false
					})
				})
			},
			// 分数事件
			changeScore() {
				const score = this.formData.score
				// 只能输入数字的验证;
				const reg = /[^\d]/g
				if (!reg.test(score)) {
					console.log('changeScore');
					this.getUserRank()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "../common/style/uc-common.scss";

	.example {
		padding: 10px;
	}

	.example-in {
		padding: 15px;
		background-color: #fff;
	}

	.sex-class {
		padding-top: 10px;
		line-height: 20px;
	}
	:deep(.is-checked){
		background-color: #f60 !important;
		border-color: #f60 !important;
	}
</style>